﻿using System;
using System.Collections.Generic;

namespace Gmantis.Controls.Chart
{
    internal class Statistics
    {
        // Fields
        private int _cnt;
        private double _max;
        private double _min;
        private double _sum;
        private double _sum2;

        // Methods
        public Statistics(IList<double> list)
        {
            int count = list.Count;
            for (int i = 0; i < count; i++)
            {
                if (IsValid(list[i]))
                {
                    double num3 = list[i];
                    if (this._cnt++ == 0)
                    {
                        this._min = this._max = num3;
                    }
                    else if (num3 < this._min)
                    {
                        this._min = num3;
                    }
                    else if (num3 > this._max)
                    {
                        this._max = num3;
                    }
                    this._sum += num3;
                    this._sum2 += num3 * num3;
                }
            }
        }

        private static bool IsValid(double val)
        {
            return (!double.IsInfinity(val) && !double.IsNaN(val));
        }

        // Properties
        public double Avg
        {
            get
            {
                if (this._cnt <= 0)
                {
                    return 0.0;
                }
                return (this._sum / ((double)this._cnt));
            }
        }

        public int Count
        {
            get
            {
                return this._cnt;
            }
        }

        public double Max
        {
            get
            {
                return this._max;
            }
        }

        public double Min
        {
            get
            {
                return this._min;
            }
        }

        public double StandardDeviation
        {
            get
            {
                return Math.Sqrt(this.Variance);
            }
        }

        public double StandardDeviationPop
        {
            get
            {
                return Math.Sqrt(this.VariancePop);
            }
        }

        public double Sum
        {
            get
            {
                return this._sum;
            }
        }

        public double Sum2
        {
            get
            {
                return this._sum2;
            }
        }

        public double Variance
        {
            get
            {
                if (this._cnt <= 1)
                {
                    return 0.0;
                }
                return ((this.VariancePop * this._cnt) / ((double)(this._cnt - 1)));
            }
        }

        public double VariancePop
        {
            get
            {
                if (this._cnt > 1)
                {
                    double num = this._sum / ((double)this._cnt);
                    return ((this._sum2 / ((double)this._cnt)) - (num * num));
                }
                return 0.0;
            }
        }
    }
}
